package com.boot.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.boot.interceptor.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bbyh
 * @since 2024-02-06
 */
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    public static final String OPTIONS = "OPTIONS";
    private static final List<String> LIMIT_FLOW_URL_LIST = new ArrayList<>();
    private static final List<String> TOKEN_EXCLUDE_URL_LIST = new ArrayList<>();
    private static final List<String> VALID_USER_URL_LIST = new ArrayList<>();
    public static String FILE_DIR;
    public static String AVATAR_DIR;

    static {
        LIMIT_FLOW_URL_LIST.add("/user/getAvatar/*");
        LIMIT_FLOW_URL_LIST.add("/user/uploadAvatar");
        LIMIT_FLOW_URL_LIST.add("/fragment/upload");
        LIMIT_FLOW_URL_LIST.add("/fragment/download");

        TOKEN_EXCLUDE_URL_LIST.add("/user/login");
        TOKEN_EXCLUDE_URL_LIST.add("/user/register");
        TOKEN_EXCLUDE_URL_LIST.add("/user/verifyToken");
        TOKEN_EXCLUDE_URL_LIST.add("/user/getAvatar/*");

        VALID_USER_URL_LIST.add("/user/logout");
        VALID_USER_URL_LIST.add("/user/uploadAvatar");
        VALID_USER_URL_LIST.add("/download/generateDownloadToken");
        VALID_USER_URL_LIST.add("/file-info/generateFile");
        VALID_USER_URL_LIST.add("/file-info/judgeFileExist");
        VALID_USER_URL_LIST.add("/dir/*");
    }

    @Resource
    private TokenInterceptor tokenInterceptor;
    @Resource
    private LimitIpInterceptor limitIpInterceptor;
    @Resource
    private LimitUserInterceptor limitUserInterceptor;
    @Resource
    private LimitFlowInterceptor limitFlowInterceptor;
    @Resource
    private ValidUserInterceptor validUserInterceptor;

    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        registry.addInterceptor(limitIpInterceptor).addPathPatterns("/**");
        registry.addInterceptor(limitUserInterceptor).addPathPatterns("/**").excludePathPatterns("/user/login", "/user/register");
        registry.addInterceptor(limitFlowInterceptor).addPathPatterns(LIMIT_FLOW_URL_LIST);
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(TOKEN_EXCLUDE_URL_LIST);
        registry.addInterceptor(validUserInterceptor).addPathPatterns(VALID_USER_URL_LIST);
    }

    @Override
    public void addCorsMappings(@NotNull CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOverflow(true);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);

        return mybatisPlusInterceptor;
    }

    @Value("${file.dir}")
    public void setFileDir(String fileDir) {
        File file = new File(fileDir);
        if (!file.exists()) {
            boolean mkdir = file.mkdirs();
            if (!mkdir) {
                log.error("文件夹：{}创建失败", fileDir);
            }
        }
        FILE_DIR = fileDir;
    }

    @Value("${avatar.dir}")
    public void setAvatarDir(String avatarDir) {
        File file = new File(avatarDir);
        if (!file.exists()) {
            boolean mkdir = file.mkdirs();
            if (!mkdir) {
                log.error("文件夹：{}创建失败", avatarDir);
            }
        }
        AVATAR_DIR = avatarDir;
    }
}
